package com.lhn.tankGame4;

import java.io.*;
import java.util.Vector;

/**
 * @author lhn
 * @date 2024-08-03 17:39
 * 记录数据和文件读写
 */

public class Recorder {

    //记录我方击毁敌人坦克数
    private static int allEnemyTankCount = 0;

    //定义IO对象
    private static FileWriter fw = null;
    private static BufferedWriter bw = null;
    private static FileReader fr = null;
    private static BufferedReader br = null;
    private static String recordFile = "E:\\DataStructureCode\\TankConflict\\test\\record.txt";

    //playerTank data
    private static Vector<Hero> heroes = null;
    //EnemyTank data
    private static Vector<EnemyTank> enemyTanks = null;

    private static Vector<Node> nodes1 = new Vector<>();
    private static Vector<Node> nodes2 = new Vector<>();

    public static PlayerAndEnemy getNodesAndEnemyTankRecord() {

        try {
            br = new BufferedReader(new FileReader(recordFile));
            String line = "";
            String currentSection = null;
            while ((line = br.readLine()) != null) {
                if ( "score:".equals(line)) {
                    allEnemyTankCount = Integer.parseInt(br.readLine());
                    System.out.println("score is " + allEnemyTankCount);
                } else if ("enemyTank:".equals(line)) {
                    currentSection = "enemyTank";



                } else if ("playerTank:".equals(line)) {
                    currentSection = "playerTank";


                } else if (currentSection != null) {
                    // 如果不是标题行，则根据当前部分添加行
                    if ("enemyTank".equals(currentSection)) {
                        String[] xyd = line.split(",");
                        Node node = new Node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]), Integer.parseInt(xyd[2]));
                        nodes2.add(node);
                    } else if ("playerTank".equals(currentSection)) {
                        String[] xyd = line.split(",");
                        Node node = new Node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]), Integer.parseInt(xyd[2]));
                        nodes1.add(node);
                    }
                }

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        PlayerAndEnemy playerAndEnemy = new PlayerAndEnemy();
        playerAndEnemy.setPlayerNode(nodes1);
        playerAndEnemy.setEnemyNode(nodes2);
        return playerAndEnemy;
    }

    public static void addCount() {
        Recorder.allEnemyTankCount ++ ;
    }

    public static void saveRecord() {
        try {
            //保存击中数
            bw = new BufferedWriter(new FileWriter(recordFile));

            bw.write("score:");
            bw.newLine();
            bw.write(new String(String.valueOf(allEnemyTankCount)));
            bw.newLine();

            bw.write("enemyTank:");
            bw.newLine();
            //保存退出时敌人坦克的位置
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);

                if (enemyTank.isLive) {
                    String enemyCoordinate = enemyTank.getX() + "," + enemyTank.getY() + "," + enemyTank.getDirection();
                    bw.write(enemyCoordinate);
                    bw.newLine();
                }
            }

            bw.write("playerTank:");
            bw.newLine();
            for (int i = 0; i < heroes.size(); i++) {
//                System.out.println("heros size is " + heroes.size());
                Hero hero = heroes.get(i);
//                System.out.println("hero is " + hero);
                if (hero != null && hero.isLive) {
                    String playerCoordinate = hero.getX() + "," + hero.getY() + "," + hero.getDirection();
                    bw.write(playerCoordinate);
                    bw.newLine();
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    public static int getAllEnemyTankCount() {
        return allEnemyTankCount;
    }

    public static void setAllEnemyTankCount(int allEnemyTankCount) {
        Recorder.allEnemyTankCount = allEnemyTankCount;
    }

    public static Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static Vector<Hero> getHeroes() {
        return heroes;
    }

    public static void setHeroes(Vector<Hero> heroes) {
        Recorder.heroes = heroes;
    }
}
